package com.zyj;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;

import java.io.IOException;

public class Lucene3DeleteDemo {


    public static void main(String[] args) throws IOException {
      //   deleteAll(LuceneUtils.getDirectory());
        delete(LuceneUtils.getDirectory(), new Term("id", "1534908020110"));
        Lucene2FindDemo.findAll(LuceneUtils.getDirectory());
      //  Lucene2FindDemo.findAll(LuceneUtils.getDirectory2());
    }


    public static void delete(Directory directory, Term term)  throws IOException {
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_47, LuceneUtils.getAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        indexWriter.deleteDocuments(term);
        indexWriter.forceMergeDeletes(); //删除索引并不是立即从磁盘删除，而是放入类回收站中，可回滚操作，需立即删除时 使用forceMergeDeletes
        System.out.println("删除索引， 当前有效数：" + indexWriter.numDocs() + "当前最大数:" + indexWriter.maxDoc());
     //   indexWriter.rollback();
        indexWriter.close();


    }



    public static void deleteAll(Directory directory) throws IOException {
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_47, LuceneUtils.getAnalyzer());
        IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
        indexWriter.deleteAll();
        indexWriter.commit();
        System.out.println("删除索引， 当前有效数：" + indexWriter.numDocs() + "当前最大数:" + indexWriter.maxDoc());
        indexWriter.close();

    }


}
